package models

import (
	"time"

	"github.com/jinzhu/gorm"
)

type Privilege struct {
	ID        int
	UserId 		int `json:"user_id"`
	Identity  string   `json:"identity"`
	CreatedBy  string `json:"first_time"`
	ModifiedBy string `json:"last_time"`

}

func GetPrivileges(pageNum int, pageSize int, maps interface{}) (tags []Tag) {
	db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&tags)
	return
}

func GetPrivilegeTotal(maps interface{}) (count int) {
	db.Model(&Tag{}).Where(maps).Count(&count)

	return
}


func (tag *Privilege) BeforeCreate(scope *gorm.Scope) error {
	scope.SetColumn("first_time", time.Now().Unix())

	return nil
}

func (tag *Privilege) BeforeUpdate(scope *gorm.Scope) error {
	scope.SetColumn("last_time", time.Now().Unix())

	return nil
}

func ExistPrivilegeByID(id int) bool {
	var tag Tag
	db.Select("id").Where("id = ?", id).First(&tag)
	if tag.ID > 0 {
		return true
	}

	return false
}

func EditPrivilege(id int, data interface{}) bool {
	db.Model(&Tag{}).Where("id = ?", id).Updates(data)

	return true
}
